package com.csw.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;


public class Demo04JoinCopy {
    public static class testMap extends Mapper<LongWritable,Text,Text,Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //判断数据来自于students.txt还是score.txt
            //根据切片路径判断
            FileSplit fs = (FileSplit) context.getInputSplit();
            //获取路径名称
            String name = fs.getPath().getName();
            if (name.contains("students.txt")){
                //以id为key  发送数据到reduce
                String id = value.toString().split(",")[0];
                //给学生信息前打上标记stu
                String line="stu"+value.toString();
                context.write(new Text(id),new Text(line));
            }else{
                String id = value.toString().split(",")[0];
                String line="sco"+value.toString();
                context.write(new Text(id),new Text(line));
            }
        }
    }
    public static class testReduce extends Reducer<Text,Text,Text,NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
                //一条students.txt数据和多条score.txt数据
            String students="";
            ArrayList<String> scores = new ArrayList<String>();
            for (Text value : values) {
                String s = value.toString();
                //去除标记
                if (s.startsWith("stu")){
                    students = s.substring(3);
                }else{
                    scores.add(s.substring(19));
            }
            }
            //关联学生信息和学生成绩
            for (String score : scores) {
                students=students+score;
            }
            context.write(new Text(students),NullWritable.get());
        }
    }
    public static void main(String[] args) throws Exception {
        //构建job
        Job job = Job.getInstance();

        //指定job名称
        job.setJobName("test");

        //指定项目名称
        job.setJarByClass(Demo04JoinCopy.class);

        //指定map类
        job.setMapperClass(testMap.class);

        //指定map输出的k和v类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //指定reduce类
        job.setReducerClass(testReduce.class);

        //指定reduce输出的k和v类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //指定输入路径
        FileInputFormat.addInputPath(job,new Path("/data/students.txt"));
        FileInputFormat.addInputPath(job,new Path("/data/score.txt"));

        //获取FileSystem对象，hdfs文件管理系统
        FileSystem fs = FileSystem.get(new Configuration());

        //指定输出路径(路径若存在则删除)
        Path path = new Path("/data/test");
        if (fs.exists(path)){
            fs.delete(path,true);
        }
        FileOutputFormat.setOutputPath(job,path);

        //启动
        job.waitForCompletion(true);
    }
}
